package com.living.player.ui.player;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.living.R;
import com.living.player.event.EventStatusPause;
import com.living.player.event.EventStatusPlay;
import com.living.player.services.MediaPlayerService;
import com.md.network.api.Audio;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class PlayInfoFragment extends Fragment implements View.OnClickListener {

    TextView tv_playing_author, tv_playing_introduce;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_palyer_info, container, false);
        tv_playing_author = view.findViewById(R.id.tv_playing_author);
        tv_playing_introduce = view.findViewById(R.id.tv_playing_introduce);
//        tv_bottom_play_title.setOnClickListener(this);
        initData();
        return view;
    }

    public void initData() {
        Intent intent = getActivity().getIntent();
        if (intent.hasExtra("audio")) {
            Audio audio = (Audio) intent.getParcelableExtra("audio");
            tv_playing_author.setText("作者: "+audio.getAuthor());
            tv_playing_introduce.setText(audio.getDesc());
        }
    }

    @Subscribe
    public void onEvent(Object event) {
//        if (event instanceof EventStatusPlay) {
//            iv_bootom_play_status.setImageResource(R.drawable.icon_player_bottom_pause);
//        } else if (event instanceof EventStatusPause) {
//            iv_bootom_play_status.setImageResource(R.drawable.icon_player_bottom_play);
//        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_bootom_play_status) {
            if (MediaPlayerService.isPlaying()) {
                EventBus.getDefault().post(new EventStatusPause());
            } else if (MediaPlayerService.isPlayable()) {
                EventBus.getDefault().post(new EventStatusPlay());
            }
        } else if (v.getId() == R.id.iv_bootom_play_view) {

        } else if (v.getId() == R.id.tv_bottom_play_title || v.getId() == R.id.tv_bottom_play_title) {
            startActivity(new Intent(getActivity(), VideoActivity.class));
//            ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//            activityManager.moveTaskToFront( , );
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }


}
